package fictional.GUI;

import java.io.IOException;

import javax.swing.JButton;

import org.jdesktop.application.Application;

import fictional.Common.CharacterItem;
import fictional.Common.IBL;
import fictional.Common.MessageItem;
import fictional.Common.Messages;
import fictional.Common.Response;
import fictional.Common.ShortCharacterItem;

/**
 * This class sends and deletes private messages between
 * characters.
 */
public class SendDeleteMessageTask extends DefaultTask
{

	private CharacterItem sender;
    private ShortCharacterItem sentTo;
    private Messages messages;
    private String message;
    private MessageItem deleteMessage;
    private JButton button;
    private ViewPrivateMessagesPopup popup;
    private boolean isFromGUI = true;;
	
	public SendDeleteMessageTask(Application application)
	{
		super(application);
	}
	
    public void setSendTask(IBL BL, CharacterItem sender, ShortCharacterItem sentTo, String message, 
            GUI gui)
    {
        this.BL = BL;
        this.sender = sender;
        this.sentTo = sentTo;
        this.gui = gui;
        this.message = message;
    }
    
    public void setSendTask(IBL BL, CharacterItem sender, ShortCharacterItem sentTo, String message, 
            GUI gui, ViewPrivateMessagesPopup popup)
    {
    	isFromGUI = false;
    	this.popup = popup;
    	setSendTask(BL ,sender, sentTo, message, gui);
    }
    
    public void setDeleteTask(IBL BL, MessageItem deleteMessage, Messages messages, GUI gui, JButton button)
    {
        this.BL = BL;
        this.gui = gui;
        this.messages = messages;
        this.deleteMessage = deleteMessage;
        this.button = button;
    }

    @Override
    protected Object doInBackground() throws IOException, InterruptedException
    {
        response = new Response();
        
        // this is a send task
        if (deleteMessage == null)
        {
            BL.sendMessage(sender, sentTo, message, response);
            
            if (response.isIs_Success())
            {
            	if (isFromGUI)
            	{
                	isSuccessMessageNeeded = true;
                	successMessage = "Message sent to " + sentTo.getName() + " succuessfully.";
            	}
            	else
            	{
            		popup.showMessageSentSuccessfullyMessage("Message sent to " + sentTo.getName() + " succuessfully.");
            	}
            }
            else
            {
            	Thread.sleep(1000);
                errorTitle = "Error Sending Message";
            }
        }
        else
        {
        	BL.deleteMessage(messages, deleteMessage, response);
            if (response.isIs_Success())
            {
                gui.deletePrivateMessageView();
                button.setText("Message Deleted");
                button.setEnabled(false);
            }
            else
            {
            	Thread.sleep(1000);
            	errorTitle = "Error Deleting Private Message";
            }
        }
        
        return null;
    }
}
